import dayjs from "dayjs";
import { ElMessage } from "element-plus";
import { getKnowledgeList, deleteKnowledge } from "@/api/lms/knowledge";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, computed, onMounted } from "vue";

export function useKnowledge() {
  const dialogFormRef = ref();
  const form = reactive({
    name: ""
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      minWidth: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "知识库名称",
      prop: "name",
      width: 200,
      align: "left"
    },
    {
      label: "embending模型",
      prop: "embeddingModel",
      width: 200,
      align: "left"
    },
    {
      label: "文件库大小",
      prop: "chunkSize",
      width: 100
    },
    {
      label: "重载大小",
      prop: "chunkOverlap",
      width: 100
    },
    {
      label: "描述",
      prop: "describe"
    },
    {
      label: "创建人",
      prop: "autoCreateInfo.creator",
      width: 80
    },
    {
      label: "创建时间",
      minWidth: 90,
      prop: "createdAt",
      formatter: ({ autoCreateInfo }) => {
        console.log(autoCreateInfo);
        return dayjs(Number(autoCreateInfo.createdAt) * 1000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 160,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });
  function handleDelete(row) {
    console.log(row);
    deleteKnowledge(row.id).then(() => {
      onSearch();
    });
  }
  function handleUpdate(row: any) {
    console.log("handleUpdate", row);
    ElMessage({
      message: "目前知识库不支持修改，需要考虑算力问题！",
      type: "warning"
    });
  }
  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
  }
  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  async function onSearch() {
    loading.value = true;
    const list = await getKnowledgeList(form.name);
    dataList.value = list;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  onMounted(() => {
    onSearch();
  });

  return {
    dialogFormRef,
    form,
    dataList,
    loading,
    pagination,
    columns,
    buttonClass,
    onSearch,
    resetForm,
    handleUpdate,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
